package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {

    private static Teacher [] teachers = new  Teacher[5];
    public  Teacher[] findAllTeacher() {

        return teachers;
    }

    public  boolean addTeacher(Teacher teacher) {
        int index = -1;

        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea == null  ){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }else {
            teachers[index] = teacher;
            return true;
        }
    }

    public void delTeacher(String id) {
       int index = getIndex(id);
        teachers[index] = null;
    }

    public void updateTeacher(Teacher tea1, String id) {
        int index = getIndex(id);

        teachers[index] = tea1;
    }

    public int getIndex(String id){
        //先找出这个id所在对象的索引值,
        int index = -1;

        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea != null && id.equals(tea.getId())){
                index = i;
            }
        }
        return index;
    }
}
